package com.tenebrous.jestic.service.framwork.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tenebrous.jestic.entity.framwork.meber.organ.OrgUser;
import com.tenebrous.jestic.entity.framwork.system.SysNotice;
import com.tenebrous.jestic.entity.framwork.system.SysNoticeRead;
import com.tenebrous.jestic.mapper.idao.framwork.system.SysNoticeMapper;
import com.tenebrous.jestic.mapper.idao.framwork.system.SysNoticeReadMapper;
import com.tenebrous.jestic.service.SuperService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class SysNoticeService extends SuperService<SysNotice> {

    @Autowired
    private SysNoticeMapper mapper;
    @Autowired
    private SysNoticeReadMapper readmapper;

    @Override
    @Autowired
    protected void setSuperMapper() {
        super.setSuperMapper(this.mapper);
    }

    public PageInfo<SysNotice> queryPagingList(Integer pageIndex, Integer pageSize, String stype, String title) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> this.mapper.selectByCondition(stype, title, null, null));
    }

    public PageInfo<SysNotice> queryPagingList(Integer pageIndex, Integer pageSize, String title, Integer published) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> this.mapper.selectByCondition(null, title, published, null));
    }

    public PageInfo<SysNotice> queryPagingList(Integer pageIndex, Integer pageSize, String title, Integer published, String userid) {
        return PageHelper.startPage(pageIndex, pageSize).doSelectPageInfo(() -> this.mapper.selectByCondition(null, title, published, userid));
    }



    @Transactional
    public int deleteByIds(String[] ids) {
        int i = 0;
        for (String id : ids) {
            if (StringUtils.isNotBlank(id)) {
                i += this.mapper.deleteByPrimaryKey(id);
            }
        }
        return i;
    }

    public int saveReadSelective(SysNoticeRead item) {
        return this.readmapper.insertSelective(item);
    }

    public int getReadedCount(String id) {
        SysNoticeRead item = new SysNoticeRead();
        item.setNoticeid(id);
        return this.readmapper.selectCount(item);
    }

    public Integer unreadCount(String userid) {
        return this.mapper.selectUnreadCount(userid);
    }

    public Integer getUserReadedCount(String noticeid, String userid) {
        return this.mapper.selectUserReadedCount(noticeid, userid);
    }
}
